<?php

namespace app\stock\controller;

use think\Controller;

class Index extends Controller
{
    protected $BusinessModel = null;
    protected $OrderModel = null;

    // 1 至 12月
    protected $timeList = [];

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
        $this->OrderModel = model('Order.Order');

        // 获取当前的年份
        $year = date('Y');

        for ($i = 1; $i <= 12; $i++) {
            // 每一次循环获取当前年份 + 某个月 strtotime => 从日期格式转换时间戳
            $time = strtotime($year . '-' . $i);

            // 获取每个月的第一天
            $startTime = strtotime(date('Y-m-01', $time));

            // 获取每个月的最后一天
            $endTime = strtotime(date('Y-m-t', $time));

            $this->timeList[] = [$startTime, $endTime];
        }
    }

    public function total()
    {
        // Vue商城的订单总数
        $OrderCount = $this->OrderModel->count();

        // Vue商城的订单总金额
        $OrderMoney = $this->OrderModel->sum('amount');

        // 已注册的客户总数
        $BusinessCount = $this->BusinessModel->count();

        $data = [
            'OrderCount' => $OrderCount,
            'OrderMoney' => $OrderMoney,
            'BusinessCount' => $BusinessCount
        ];

        $this->success('查询成功', null, $data);
    }

    public function business()
    {
        // 查询每年12个月注册的客户（未认证，已认证）

        // 存放数据
        $BusinessData = [];

        foreach ($this->timeList as $time) {
            // 未认证
            $where = [
                'auth' => 0,
                'createtime' => ['between', $time] // between 查询范围之间的数据
            ];

            $noCertified[] = $this->BusinessModel->where($where)->count();

            // 已认证
            $where2 = [
                'auth' => 1,
                'createtime' => ['between', $time]
            ];

            $Certified[] = $this->BusinessModel->where($where2)->count();
        }

        $BusinessData = [
            'noCertified' => $noCertified,
            'Certified' => $Certified
        ];

        $this->success('查询成功', null, $BusinessData);
    }

    public function visit()
    {
        $VisitData = [];

        foreach ($this->timeList as $item) {
            $where = [
                'createtime' => ['between', $item]
            ];

            $VisitData[] = model('Business.Visit')->where($where)->count();
        }

        $this->success('查询成功', null, $VisitData);
    }

    // 客户领取
    public function receive()
    {
        // 申请 apply 分配 allot 回收 recovery

        foreach ($this->timeList as $item) {
            $ApplyWhere = [
                'status' => 'apply',
                'applytime' => ['between', $item]
            ];

            $ApplyData[] = model('Business.Receive')->where($ApplyWhere)->count();

            $AllotWhere = [
                'status' => 'allot',
                'applytime' => ['between', $item]
            ];

            $AllotData[] = model('Business.Receive')->where($AllotWhere)->count();

            $RecoveryWhere = [
                'status' => 'recovery',
                'applytime' => ['between', $item]
            ];

            $RecoveryData[] = model('Business.Receive')->where($RecoveryWhere)->count();
        }

        $this->success('查询成功', null, ['ApplyData' => $ApplyData, 'AllotData' => $AllotData, 'RecoveryData' => $RecoveryData]);
    }

    public function order()
    {
        // 存放所有的数据的数组
        $OrderData = [];

        // 查询已支付
        $paid = $this->OrderModel->where(['status' => 1])->count();

        // 已发货
        $shipped = $this->OrderModel->where(['status' => 2])->count();

        // 已收货
        $received = $this->OrderModel->where(['status' => 3])->count();

        // 已完成
        $completed = $this->OrderModel->where(['status' => 4])->count();

        $OrderData = [
            [
                'name' => '已支付',
                'value' => $paid,
            ],
            [
                'name' => '已发货',
                'value' => $shipped,
            ],
            [
                'name' => '已收货',
                'value' => $received,
            ],
            [
                'name' => '已完成',
                'value' => $completed
            ]
        ];

        $this->success('查询成功', null, $OrderData);
    }

    public function source()
    {
        // 存放所有的数据的数组
        $SourceData = [];

        // 获取所有的客户来源
        $Source = model('Business.Source')->select();

        // 循环
        foreach ($Source as $item) {
            $SourceData[] = [
                'name' => $item['name'],
                'value' => $this->BusinessModel->where(['sourceid' => $item['id']])->count()
            ];
        }

        $this->success('查询成功', null, $SourceData);
    }
}
